package com.catchu.pattern.creational.原型模式;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理
 */
public class OrderManage {

    /**
     * 根据原始订单创建出新的订单
     * @param srcOrder
     */
    public void createNewOrder(List<Order> srcOrder) throws CloneNotSupportedException{
        System.out.println("原始单据");
        for(Order order : srcOrder){
            System.out.println(order.toString());
            System.out.println("-----------短分割线--------");
        }

        List<Order> descOrders = new ArrayList<>();
        for(Order order : srcOrder){
            Order descOrder = order.clone();
            if(descOrder instanceof SalesOrder){
                //如果是销售单，根据商品名称获取单价
                ((SalesOrder) descOrder).setUnitPrice(getUnitPrice(((SalesOrder) descOrder).getProductName()));
            }
            descOrders.add(descOrder);
        }

        System.out.println("------------------长分割线-----------");
        System.out.println("新单据");
        for(Order order : descOrders){
            System.out.println(order.toString());
            System.out.println("-----------短分割线--------");
        }
    }

    /**
     * 获取产品单价（demo）
     * @param productName 产品名
     * @return 单价
     */
    private float getUnitPrice(String productName){
        // 单价
        float unitPrice = 0.0f;
        if("婴儿车".equals(productName)){
            unitPrice = 999.0f;
        }
        return unitPrice;
    }
}
